﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no" />
<link rel="shortcut icon" href="/Images/ico/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="/css/swiper.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<link type="text/css" rel="stylesheet" href="/layui/css/layui.css"  media="all">
<script type="text/javascript" src="/layui/layui.js" charset="utf-8"></script>
<script src="/js/jquery.min.js" type="text/javascript"></script>
<script src="/js/swiper.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/custom.js"></script>
<script type="text/javascript" src="/js/layui-post.js"></script>
<style>
.layui-layer-title{padding:0!important;}
</style>
</head>
<body>
<div class="ng-scope">
    <header class="ng-scope" id="gwc">
        <h4 class="title-order">
            <a class="title-top" href="javascript:window.history.back();"></a>
            <a class="title-center">我的地址</a>
            <a class="title-right" href="/index"></a>
        </h4>
    </header>
    <div class="ng-scope" style="margin-bottom: 50px;margin-top: 50px;overflow-x: hidden;">
        <div class="list">
            <ul class="branch" id="branch3"></ul>
        </div>
    </div>
    <div style="width: 100%; position: fixed; text-align: center; bottom: 0px; height: 50px; line-height: 49px; font-size: 15px; font-weight: bold; background: #e7e7e7;" onclick="addAddress()">
        <img src="img/num_r.png" style="width: 20px;margin-right: 5px;" class="img-responsive num_r" alt="..." />新增收货地址
    </div>
</div>
<div class="cartempty ng-scope ng-hide">
    <h3>暂无数据</h3>
</div>
</body>
<script>
$.ajaxSetup({contentType: "application/json; charset=utf-8"});
var userInfo = $.parseJSON(sessionStorage.getItem("userInfo"));
layui.use(['layer','form','element','util'], function(){
    getAddressList()
})
function getAddressList(){
    showData();
}
function showData(){
    $.post("/cateringAddress/select",JSON.stringify({userId:userInfo.userId,isDel:0}),function(res){
        $("#branch3").html("");
        if(res.data.length == 0) $(".cartempty").removeClass("ng-hide")
        else $(".cartempty").addClass("ng-hide")
        for(var i in res.data){
            $("#branch3").append(
                '<div class="addressbox on">' +
                '<div class="addressbox_1">' +
                '<p class="p1">收货人：'+res.data[i].person+'<span>'+jiamiphone(res.data[i].telPhone)+'</span></p>' +
                '<p class="p2">'+res.data[i].address.replaceAll("-","")+' '+res.data[i].detail+'</p>' +
                '</div>' +
                '<div class="addressbox_2">' +
                '<div class="ad1">' +
                '<div class="ad1_1">' +
                '<div class="gwccheck '+(res.data[i].isDefault == 1 ? 'on':'')+'"></div>' +
                '</div>' +
                '<div class="ad1_2" onclick="setIsDef(\''+res.data[i].sysId+'\',\''+res.data[i].isDefault+'\')">设为默认</div>' +
                '</div>' +
                '<div class="ad2" onclick="delAddress(\''+res.data[i].sysId+'\',\''+res.data[i].isDefault+'\')">' +
                '<div class="ad2_1"><img src="images/del.png" /></div>' +
                '<div class="ad2_2">删除</div>' +
                '</div>' +
                '<div class="ad2 mr" onclick="addressEdit(\''+res.data[i].sysId+'\')">' +
                '<a>' +
                '<div class="ad2_1"><img src="images/edit.png" /></div>' +
                '<div class="ad2_2">编辑</div>' +
                '</a>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="w100" style="display:'+(res.data[i].isDefault == 1 ? 'block':'none')+'">' +
                '<img src="images/addressline.png" />' +
                '</div>'+
                '<div class="kbox"></div>'
            )
            if(1 === res.data[i].isDefault) $("#"+res.data[i].sysId).prop("checked",true);
        }
    })
}
function jiamiphone(phone){
    var hiddenPhone = ""; //定义变量存放隐藏后的电话号码
    if (phone.length >= 10) { //判断长度大于等于10位时才进行处理
        for(var i=0;i<3;i++){ //将第4到7位之前的字符保持不变
            hiddenPhone += phone[i];
        }
        for(var i=3;i<=11;i++){ //将第8到9位之前的字符改成 *
            hiddenPhone += "*";
        }
    }
    return hiddenPhone;
}
function setIsDef(id,isdef){
    if(isdef == 1) layer.msg("已经设置默认地址",{icon:0,time:1500})
    else{
        layer.confirm('确认设置为默认吗？',function(index){
            $.post("/cateringAddress/update",JSON.stringify({sysId:id,isDefault:1}),function(res){
                if(res.success) layer.msg(res.msg,{icon:1,time:1500},function(){
                    showData();
                });
            })
        })
    }
}
function addAddress(){
    parent.layer.open({
        type:2,
        area: ['100%', '100%'],
        title:["新增收货地址", 'text-align:center;font-size: 16px; font-weight: bold;padding-left: 50px;'],
        closeBtn:1,
        btnAlign: 'c',
        offset: 'b',
        anim: 0,
        skin:'layui-layer-nobg',
        shadeClose:true,
        content:"/addressadd",
        scrollbar:false,
    })
}
function addressEdit(id){
    parent.layer.open({
        type:2,
        area: ['100%', '100%'],
        title:["修改收货地址", 'text-align:center;font-size: 16px; font-weight: bold;padding-left: 50px;'],
        closeBtn:1,
        btnAlign: 'c',
        offset: 'b',
        anim: 0,
        skin:'layui-layer-nobg',
        shadeClose:true,
        content:"/addressedit?sysId="+id,
        scrollbar:false,
    })
}
function delAddress(id,isDefault){
    if(isDefault == 1) layer.msg("默认地址不能删除",{icon:2,time:1500})
    else {
        layer.confirm('确认要删除吗？',function(index){
            $.post("/cateringAddress/update",JSON.stringify({sysId:id,isDel:1}),function(res){
                layer.msg(res.msg,{icon:(res.success === true ? 1 : 0),time:1500},function(){
                    showData();
                });
            })
        })
    }
}
</script>
</html>
